package com.example.demo.service;

import com.example.demo.mapper.ForumMapper;
import com.example.demo.model.Forum;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ForumService {

    private final ForumMapper forumMapper;

    // 构造函数注入 ForumMapper，实现依赖注入
    public ForumService(ForumMapper forumMapper) {
        this.forumMapper = forumMapper;
    }

    // 获取所有论坛帖子
    public List<Forum> getAllForums() {
        return forumMapper.selectAllForums();
    }

    // 根据ID查询论坛帖子
    public Forum getForumById(Long id) {
        return forumMapper.selectForumById(id);
    }

    // 根据用户名查询论坛帖子
    public List<Forum> getForumByUsername(String username) {
        return forumMapper.selectForumByUserName(username);
    }

    // 新增论坛帖子
    public void createForum(Forum forum) {
        // 设置创建时间和回复时间为当前时间
        LocalDateTime now = LocalDateTime.now();
        forum.setCreateTime(now);
        forum.setReplyTime(now);
        forumMapper.insertForum(forum);
    }

    // 删除论坛帖子
    public void deleteForum(Long id) {
        forumMapper.deleteForumById(id);
    }

    // 更新论坛帖子内容和回复时间
    public void updateForum(Forum forum) {
        forum.setReplyTime(LocalDateTime.now());
        forumMapper.updateForum(forum);
    }
}